package com.CGFrame.struts.bean.log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import com.CGFrame.struts.POJOs.Loghistory;
import com.CGFrame.struts.POJOs.Reguser;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.form.loghistory.QueryForm;

public class LogHistoryBean {
	
	private ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
	public final static int LOGIN = 1;
	public final static int QUERY = 2;
	public final static int MODIFY= 3;
	public final static int ADD   = 4;
	public final static int DELETE= 5;
	public final static int OM    = 6;
	public final static int ERROR = 7;
	/** 
	 * @param reguser 
	 * @param operateType 1:login. 2:query. 3:modify. 4:add. 5:delete. 6:OM.
	 * @param operation
	 * @param objectType TableName
	 * @param objectId
	 */
	public void save(Reguser reguser, Integer operateType, String operation, String objectType, Integer objectId)
	{
		 Loghistory log =new Loghistory( reguser,  operateType,  operation, new Date(),  objectType,  objectId);	
		 pop.saveObject(log);
	}
	
	public void saveByReguserId(String regUserId ,Integer operateType, String operation, String objectType, Integer objectId)
	{
		Reguser reguser = (Reguser)pop.load("Reguser", Integer.parseInt(regUserId) );
		if (reguser == null)
		{
			this.save(reguser, this.ERROR, operation, objectType, objectId);
		}
		this.save(reguser, operateType, operation, objectType, objectId);
	}
	
	public List Query(QueryForm queryForm)
	{
		
		StringBuffer hqlQuery = new StringBuffer("from Loghistory  as o where 1=1 " ); 
		if (queryForm.getRegUserId()!=0)
		{
			hqlQuery.append(" and o.reguser.regUserId =").append(queryForm.getRegUserId());
		}
		if (queryForm.getOperateType()!=0)
		{
			hqlQuery.append(" and operateType = ").append(queryForm.getOperateType());
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		Date startTime =null;
		Date endTime = null;
		try {
			startTime = df.parse(queryForm.getStartTime());
			endTime   = df.parse(queryForm.getEndTime());
			hqlQuery.append(" and createTime between :l and :r");
			
			Query query = pop.getQuery(hqlQuery.toString());
			query.setParameter("l", startTime);
			query.setParameter("r",endTime);
			return query.list();
		} catch (ParseException e) {
		
		}
	    return pop.Query(hqlQuery.toString());
//		if ( startTime!=null )
//		{
//			 hqlQuery.append( " and o.createTime >= '"+startTime+"'"  );
//		}
//		if ( endTime!=null )
//		{
//			 hqlQuery.append( " and o.createTime <= '"+startTime+"'"  );
//		}
		
		
// DEMO！！！！！！！！！！！！！！		
//		 1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//		 2 Date ld = df.parse("2006-01-01");
//		 3 Calendar rd = Calendar.getInstance();
//		 4 rd.setTime(ld);
//		 5 rd.set(Calendar.HOUR_OF_DAY, 23);
//		 6 rd.set(Calendar.MINUTE, 59);
//		 7 rd.set(Calendar.SECOND, 59);
//		 8 Query query = sessoin.createQuery("from Order where date between :l and :r");
//		 9 query.setParameter("l", ld);
//		10 query.setParameter("r", rd.getTime());
//		11 return query.list();
		
	}
	public static void main(String[] args)
	{
		Reguser reguser=(Reguser) ProjectOperateFactory.getprojectOperate().load("Reguser", 1);
		LogHistoryBean logBean = new LogHistoryBean();
		logBean.save(reguser, 1, "192.168.20.214", null, null);
	}
	
}